<?php

class AdminUserService
{
    /*
     * 验证用户名是否合法
     *
     */
    public static function vaildAdminUser($adminuser)
    {
        //英文、数字、下划线5-20位字符
        $preg='/^[\w\_]{4,20}$/u';

        if(preg_match($preg,$adminuser)==false)
        {
            throw new Exception("用户名不合法");
        }

    }

    /*
     * 验证密码是否合法
     *
     */
    public static function vaildAdminPassword($password)
    {
        //英文、数字6-20位字符
        $preg='/^[a-zA-Z\d_]{6,20}$/';

        if(preg_match($preg,$password)==false)
        {
            throw new Exception("密码不合法");
        }

    }

    /*
     * 登录操作
     *
     */
    public static function login($username,$password)
    {

        $adminuser=AdminUser::model()->find("user_name=:user_name",array(":user_name" => $username));


        //用户是否存在
        if(empty($adminuser))
        {
            throw new Exception("用户不存在");
        }



        //密码是否正确
        if($adminuser->password == $password)
        {
            return $adminuser;

        }
        else
        {
            throw new Exception("密码错误");
        }


    }

    /*
     * 模糊adminuser
     *
     */
    public  static function formatAdminUser($adminuser)
    {
        $adminuser->password = "";
        return $adminuser;
    }


    /*
     * 添加用户
     *
     */
    public static function addAdminUser($user_name,$role_id,$real_name)
    {
        $role = AdminRole::model()->findByPK($role_id);

        //检查角色
        Util::checkExistAndNotDelete($role);

        //检查用户名是否有重复
        //$adminuser = AdminUser::model()->find("user_name1='"+$user_name+"'");
        $adminuser = AdminUser::model()->find("user_name ='".$user_name."'");

        if(empty($adminuser)==false)
        {
            if($adminuser->is_delete == 0)
                throw new Exception("有相同用户名已经存在 ".$adminuser->user_id);
        }

        //创建
        if(empty($adminuser))
        {
            $adminuser = new AdminUser();
            $adminuser->user_name = $user_name;
            $adminuser->real_name = $real_name;
            $adminuser->password = MsgConst::ADMIN_USER_DEFAULT_PASSWORD;
            $adminuser->role_id = $role_id;

            Util::initObjData($adminuser);

        }
        else
        {
            Util::recoverObjData($adminuser);

        }
        Util::saveObjData($adminuser);


    }

    //删除用户

    public static function deleteUser($user_id)
    {
        $adminuser = AdminUser::model()->findByPK($user_id);

        if(empty($adminuser)==true)
            throw new Exception("用户不存在");
        if($adminuser->is_delete == 1)
            throw new Exception("用户已被删除");

        if($user_id == MsgConst::ADMIN_DEFAULT_NUM)
            throw new Exception("不能删除默认管理员");


        Util::deleteObjData($adminuser);
        Util::saveObjData($adminuser);


    }


    //重置密码

    public static function resetPassword($user_id)
    {
        $adminuser = AdminUser::model()->findByPK($user_id);

        Util::checkExistAndNotDelete($adminuser);


        $adminuser->password = MsgConst::ADMIN_USER_DEFAULT_PASSWORD;
        Util::updateObjData($adminuser);

        Util::saveObjData($adminuser);


    }


    //删除用户

    public static function changePassword($user_id,$password,$oldpwd)
    {
        $adminuser = AdminUser::model()->findByPK($user_id);

        Util::checkExistAndNotDelete($adminuser);

        if($adminuser->password != $oldpwd)
            throw new Exception("原密码错误");


        $adminuser->password = $password;
        Util::updateObjData($adminuser);

        Util::saveObjData($adminuser);


    }

    //修改用户信息

    public static function changeUserInfo($user_id,$role_id,$real_name)
    {
        $adminuser = AdminUser::model()->findByPK($user_id);

        Util::checkExistAndNotDelete($adminuser);


        $adminuser->role_id = $role_id;
        $adminuser->real_name = $real_name;
        Util::updateObjData($adminuser);

        Util::saveObjData($adminuser);


    }

















}


?>